﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using C1.Web.Wijmo.Controls.C1TreeView;

namespace weba
{
    public partial class treewz : System.Web.UI.Page
    {
        sjklqDataContext j = new sjklqDataContext();
        string pg;
        protected void Page_Load(object sender, EventArgs e)
        {
            pg = Request.QueryString["pid"];
            if (!IsPostBack)
            {
                getroot();
            }
          
        }

        private void getroot()
        {
            List<dituwz> d = j.dituwz.Where(h => h.pid == 0).ToList();
            foreach (dituwz z in d)
            {
                TreeNode t = new TreeNode(z.name.ToString(), z.Id.ToString());
                t.ImageToolTip = "10";
                t.SelectAction = TreeNodeSelectAction.SelectExpand;
                t.NavigateUrl = "treewz.aspx?pid="+z.Id.ToString()+"&zm=10";
              //      t.Target = "ybf";
                   
                TreeView1.Nodes.Add(t);
                getchilds(t);
            }
        }
        private bool checkexp(TreeNode td)
        {
            if (td.Value==pg)
            {
                return true;
            }
            List<dituwz> d = j.dituwz.Where(h => h.pid.ToString() == td.Value).ToList();
            if (d.Count == 0)
            {
                return false;
            }
            bool ck=false;
             foreach (dituwz z in d)
            {
                if (z.Id.ToString() == pg)
                {
                    ck = true;
                }
               else
                {
                  //  int a = 0;
                    checkexpc(z, ref ck);
                }
             }
             return ck;
          }
          private void checkexpc(dituwz td, ref bool k)
        {
             
            List<dituwz> d = j.dituwz.Where(h => h.pid.ToString() == td.Id.ToString()).ToList();
            if (d.Count == 0)
            {
                return;
            }
              foreach (dituwz z in d)
            {
                if (z.Id.ToString() == pg)
                {
                    k = true;
                }
               else
                {
                  //  int a = 0;
                    checkexpc(z, ref k);
                }
             }
          }
        private void getchilds(TreeNode td)
        {
          //  throw new NotImplementedException();
           if (checkexp(td))
           {
               td.Expand();
           }
           
            
            List<dituwz> d = j.dituwz.Where(h => h.pid.ToString() == td.Value).ToList();
            if (d.Count==0)
            {
                return;
            }
            foreach (dituwz z in d)
            {
                //if (b)
                //{
                //    TreeNode t = new TreeNode(z.name.ToString(), z.Id.ToString());
                //    t.NavigateUrl = "treewz.aspx?pid=" + z.Id.ToString();
                //    //    t.Target = "ybf";
                //    t.SelectAction = TreeNodeSelectAction.SelectExpand;
                //    t.Expand();
                //  //  td.Expand();
                //    //  t.Target = "ybf";
                //    td.ChildNodes.Add(t);
                //    getchilds(t);
                //} 
                //else
                //{
 TreeNode t = new TreeNode(z.name.ToString(), z.Id.ToString());
 t.NavigateUrl = "treewz.aspx?pid=" + z.Id.ToString() + "&zm=" + (int.Parse(td.ImageToolTip) + 3).ToString();
            //    t.Target = "ybf";
                t.ImageToolTip = (int.Parse(td.ImageToolTip) + 3).ToString();
                t.SelectAction = TreeNodeSelectAction.SelectExpand;
                t.Collapse();
              //  t.Target = "ybf";
                td.ChildNodes.Add(t);
                getchilds(t);
              //  }
               
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("prga.aspx");
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
          //  Response.Write(" <script language=javascript >  $('#fm').src(treewz.aspx) </script> ");
         //   Response.End();
          //  Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<script>document.getElementById('fm').src='treewz.aspx')</script>", true);
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
          //ClientScript.RegisterClientScriptBlock(this.Page.GetType(),"","$('#fm').src('" + this.TreeView1.SelectedNode.NavigateUrl+"')",true);
           // Response.End();
            
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string sf = C1AutoComplete1.Text.ToString();
            Response.Redirect("treewz.aspx?pid="+ sf + "&zm=18");
        }

        
    }
}